﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4T.BLL;
using _4T.Data;
using _4T.Data.IDataContext;
using CMS.Core.Libs;


namespace _4T.ASCX
{
    public partial class ProfileApproveControl : System.Web.UI.UserControl, ICMSApproveProfile
    {
        #region Contructor
        CMSConsiderAndDecide cmsConsiderAndDecide;
        private List<string> imageArrayPath = new List<string>();
        private List<string> videoArrayPath = new List<string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["imageArrayPath"] = imageArrayPath;
                Session["videoArrayPath"] = videoArrayPath;
            }
            BindingData();
        }
        #endregion

        #region Private

        private void BindingData()
        {
            cmsConsiderAndDecide = new DAL.ConsiderAndDecideDAL().GetConsiderAndDecideByID(ConsiderAndDecideID);
            txtDiseaseName.Text = cmsConsiderAndDecide.DiseaseName;
            txtPhaseOfDisease.Text = cmsConsiderAndDecide.PhaseOfDisease;
            if (cmsConsiderAndDecide.IsTreat)
            {
                rdoIsTreatYes.Checked = true;
                rdoIsTreatNo.Checked = false;

            }
            else
            {
                rdoIsTreatNo.Checked = true;
                rdoIsTreatYes.Checked = false;
            }

            if (cmsConsiderAndDecide.IsRecover)
            {
                rdoIsRecoverYes.Checked = true;
                rdoIsRecoverNo.Checked = false;
            }
            else
            {
                rdoIsRecoverNo.Checked = true;
                rdoIsRecoverYes.Checked = false;
            }


            txtAnticipateSpend.Text = cmsConsiderAndDecide.AnticipateSpend;
            txtTreatTime.Text = cmsConsiderAndDecide.TreatTime;
            txtCommentOfDoctor.Text = cmsConsiderAndDecide.CommentOfDoctor;

            if (cmsConsiderAndDecide.IsPoor)
            {
                rdoIsPoorYes.Checked = true;
                rdoIsPoorNo.Checked = false;
            }
            else
            {
                rdoIsPoorNo.Checked = true;
                rdoIsPoorYes.Checked = false;
            }

            if (cmsConsiderAndDecide.IsDirectLabour)
            {
                rdoIsDirectLabourYes.Checked = true;
                rdoIsDirectLabourNo.Checked = false;
            }
            else
            {
                rdoIsDirectLabourNo.Checked = true;
                rdoIsDirectLabourYes.Checked = false;
            }

            txtCauseIll.Text = cmsConsiderAndDecide.CauseIll;
            //
            //
            //
            string profileInfoCode = cmsConsiderAndDecide.ProfileName;
            // Load patient info
            PatientInfoBLL patientBLL = new PatientInfoBLL();
            _4T.Data.PatientInfo patient = patientBLL.GetPatientInfo(profileInfoCode);
            if (patient != null)
            {
                lbPatientName.Text = patient.PatientName;
                lbDOB.Text = patient.Birthday.ToShortDateString();
                ddlGender.SelectedValue = patient.Gender;
                lbIDNumber.Text = patient.IDNumber;
                lbPermAddr.Text = patient.PermanentAddress;
                lbTempAddr.Text = patient.TemporaryAddress;
                lblMobileNumber.Text = patient.MobileNumber;
                lbPhoneNumber.Text = patient.PhoneNumber;
                lbRepresentativeName.Text = patient.EmergencyRepresentative;
                lbRepresentativePhone.Text = patient.RepresentativePhoneNumber;
                lbLocalContactName.Text = patient.LocalContactPoint;
                lbLocalContactPhone.Text = patient.LocalContactPointPhoneNumber;
                lbMedicalStaff.Text = patient.LocalMedicalStaff;
                lbMedicalStaffPhone.Text = patient.LocalMedicalStaffPhoneNumber;
                lbMedicalFacility.Text = patient.MedicalFacility;
                lbNote.Text = patient.Note;
            }

            // Load medical info
            MedicalInfoBLL medicalBLL = new MedicalInfoBLL();
            _4T.Data.MedicalInfo medical = medicalBLL.GetMedicalInfo(profileInfoCode);
            if (medical != null)
            {
                lblDiagnosis.Text = medical.Diagnosis;
                lbMedicalTime.Text = medical.MedicalTime.ToShortDateString();
                lbTreatmentMethod.Text = medical.TreatmentMethod;
                lbSituation.Text = medical.Situation;
                lbSupport.Text = medical.Support;
            }

            // Load info supplier info
            InfoSupplierInfoBLL infoSupplierBLL = new InfoSupplierInfoBLL();
            _4T.Data.InfoSupplierInfo supplier = infoSupplierBLL.GetInfoSupplierInfo(profileInfoCode);
            if (supplier != null)
            {
                lbInfoSupplierName.Text = supplier.SupplierName;
                lbInfoSupplierPhone.Text = supplier.PhoneNumber;
                lbEmail.Text = supplier.Email;
            }
            /*if (Request.QueryString["pid"] != null)
            {
                result = new ConsiderAndDecideBLL().GetConsiderAndDecideByID(Guid.Parse(Request.QueryString["pid"]));
                lblProfileCode.Text = result.ProfileName;

                var patientObj = new PatientInfoBLL().GetPatientInfo(result.ProfileName);
                if (patientObj != null)
                {
                    lblProfileName.Text = !string.IsNullOrEmpty(patientObj.PatientName) ? patientObj.PatientName : string.Empty;
                    lblDateOfBirth.Text = patientObj.Birthday.ToString();
                    lblCMND.Text = patientObj.IDNumber;
                    lblGender.Text = patientObj.Gender;
                    lblPhone.Text = patientObj.MobileNumber;
                    lblAdress1.Text = patientObj.PermanentAddress;
                    lblAdress2.Text = patientObj.TemporaryAddress;

                }

                lblFilterName.Text = result.ConsiderAndDecideName;
                lblFiltDate.Text = result.CreatedDate.ToString();
                lblCommentOfFilter.Text = result.CommentOfConsider;
            }
            */
        }

        #endregion


        #region ICMSConsiderAndDecide
        public Guid ApproveProfileID
        {
            get
            {
                return Guid.NewGuid();
            }
            set
            {
                ApproveProfileID = value;
            }
        }

        public string ApproveName
        {
            get
            {
                return Session["User"] != null ? Session["User"].ToString() : string.Empty;
            }
            set
            {
                ApproveName = value;
            }
        }

        public byte Status
        {
            get
            {
                return rdoChecked.Checked ? (byte)ApproveProfileStatus.Approve : rdoNonChecked.Checked ? (byte)ApproveProfileStatus.NonApprove : (byte)ApproveProfileStatus.NeedMoreInfomation;
            }
            set { Status = value; }
        }

        public string Comment
        {
            get
            {
                return !string.IsNullOrEmpty(txtComment.Text) ? txtComment.Text : string.Empty;
            }
            set { Comment = value; }
        }

        private string _imageUrl;
        public string ImageUrl
        {
            get
            {
                imageArrayPath = (Session["imageArrayPath"] as List<string>);
                if (imageArrayPath != null && imageArrayPath.Count > 0)
                {
                    foreach (var item in imageArrayPath)
                    {
                        _imageUrl += item;
                        break;
                    }
                    return _imageUrl;
                }
                else
                {
                    return string.Empty;
                }
            }
            set { _imageUrl = value; }
        }

        private string _videoUrl;
        public string VideoUrl
        {
            get
            {
                videoArrayPath = (Session["videoArrayPath"] as List<string>);
                if (videoArrayPath != null && videoArrayPath.Count != 0)
                {
                    foreach (var item in videoArrayPath)
                    {
                        _videoUrl += item;
                        break;
                    }
                    return _videoUrl;
                }
                else
                {
                    return string.Empty;
                }
            }
            set { _videoUrl = value; }
        }

        public string ProfileID
        {
            get { return cmsConsiderAndDecide.ProfileName; }
            set { }
        }

        public Guid ConsiderAndDecideID
        {
            get { return Guid.Parse(Request.QueryString["pid"]); }
            set { }
        }

        public string BenefitMoney
        {
            get
            {
                return !string.IsNullOrEmpty(txtMoney.Text) ? txtMoney.Text : string.Empty;
            }
            set
            {
                BenefitMoney = value;
            }
        }

        public DateTime EditedTime
        {
            get
            {
                DateTime editime = new DateTime();

                try
                {
                    if (!string.IsNullOrEmpty(txtCreatedDate.Text))
                        editime = Convert.ToDateTime(txtCreatedDate.Text);
                }
                catch
                {
                    editime = DateTime.Now;
                }

                return editime;
            }
            set
            {
            }
        }
        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string sessionVideoUrl = VideoUrl;
            string sessionImageUrl = ImageUrl;
            Session["imageArrayPath"] = null;
            Session["videoArrayPath"] = null;
            int updateStatus = 0;
            int updateStage = (int)ProfileStage.Payment;
            string money = string.Empty;
            try
            {
                money = (int.Parse(txtMoney.Text.ToString())).ToString();
            }
            catch
            {
                money = "0";
            }
            if (rdoNonChecked.Checked == true)
            {
                updateStatus = 2;
                updateStage = updateStage - 1;
            }
            ProfileStatus profileStatus = new ProfileStatus
            {
                ProfileCode = cmsConsiderAndDecide.ProfileName,
                Approver = Session["User"] != null ? Session["User"].ToString() : string.Empty,
                Stage = updateStage,
                Status = updateStatus,
                Money = money
            };

            DateTime createdDate = new DateTime();

            try
            {
                if (!string.IsNullOrEmpty(txtCreatedDate.Text))
                    createdDate = Convert.ToDateTime(txtCreatedDate.Text);
            }
            catch
            {
                createdDate = DateTime.Now;
            }
            //int resultStageLink = new ProfileStageLinkBLL().InsertProfileStageLink(this.ProfileID, (int)ProfileStage.Approve, sessionVideoUrl, null, sessionImageUrl, string.Empty, createdDate);
            if (new ApproveProfileBLL().AddApproveProfile(ApproveProfileID, ApproveName, Status, Comment, sessionImageUrl, sessionVideoUrl, EditedTime, ProfileID, ConsiderAndDecideID, BenefitMoney) > 0 && new ProfileStatusBLL().UpdateProfileStatus(profileStatus))
            {
                HttpContext.Current.Response.Redirect("~/ProfileApproveList.aspx");
            }
        }

        #region Event


        #endregion

    }
}